package mq.demo.priority;

import cn.itlaobing.Util.ConnectionUtil;
import cn.itlaobing.Util.ConnectionUtil;
import com.rabbitmq.client.AMQP.BasicProperties;
import com.rabbitmq.client.AMQP.BasicProperties.Builder;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;

import java.io.Serializable;
import java.util.Random;

/**
 * @author mifei
 * @create 2018年04月12日
 **/
public class Send {
    private static String exchange_name = "priority_direct";

    public static void prioritySend(Serializable mes) throws Exception {
        Connection conn = ConnectionUtil.getConnection();
        Channel channel = conn.createChannel();
        channel.exchangeDeclare(exchange_name, "direct");
        //发送10条消息
        for (int i = 0; i < 10; i++) {
            //随机设置消息优先级
            Builder properties = new BasicProperties.Builder();
            int priority = new Random().nextInt(10);
            properties.priority(priority);//建议0~255，超过貌似也没问题
            String _mes = mes.toString() + i;
            channel.basicPublish(exchange_name, "", properties.build(), _mes.getBytes());
            System.out.println(priority + " " + _mes);
        }
        channel.close();
        conn.close();
    }

    public static void main(String[] args) throws Exception {
        prioritySend("priority send:hello world!");
    }
}
